//
//  Hx_ScrollVIew.m
//  05-引导页
//
//  Created by 韩旭 on 16/7/20.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "Hx_ScrollVIew.h"

#define WIDETH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
#define COLORAUTO [UIColor colorWithRed:(arc4random()%256/255.0) green:(arc4random()%256/255.0) blue:(arc4random()%256/255.0) alpha:1.0]

#define kWidth [UIScreen mainScreen].bounds.size.width
#define kHeight [UIScreen mainScreen].bounds.size.height

@interface Hx_ScrollVIew ()<UIScrollViewDelegate>
@property (nonatomic, strong) NSArray *arrDataSource;
@property (nonatomic, assign) CGRect smallframe;
@property (nonatomic, assign) NSInteger zoomMin;
@property (nonatomic, assign) NSInteger zoomMax;
@property (nonatomic, assign) CGRect scrollFrame;
@property (nonatomic, assign) CGRect pageFrame;
@property (nonatomic, strong) UIColor *pageTitinColor;
@property (nonatomic, strong) UIColor *currPageColor;
@property (nonatomic, assign) id target;
@property (nonatomic, assign) id targetControl;
@property (nonatomic, strong) UIPageControl *pageContrl;
@property (nonatomic, strong) UIScrollView *scrollView;
@end
@implementation Hx_ScrollVIew
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self packWithScrollViewFrame:_scrollFrame arrDataSource:_arrDataSource smallScrollViewframe:_smallframe zoomMin:_zoomMin zoomMax:_zoomMax pageFrame:_pageFrame pagetitinColor:_pageTitinColor currPageColor:_currPageColor];
    }
    return self;
}
- (void)packWithScrollViewFrame:(CGRect)scrollViewframe
                  arrDataSource:(NSArray *)arrDataSource
           smallScrollViewframe:(CGRect)smallScrollViewframe
                        zoomMin:(NSInteger)zoomMin
                        zoomMax:(NSInteger)zoomMax
                      pageFrame:(CGRect)pageFrame
                 pagetitinColor:(UIColor *)pagetitinColor
                  currPageColor:(UIColor *)curPageColor {
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:scrollViewframe];
    _scrollView.delegate = self;
    _scrollView.showsHorizontalScrollIndicator = YES;
    _scrollView.pagingEnabled = YES;
    _scrollView.bounces = YES;
    for (int i = 0; i < arrDataSource.count; i++) {
        NSLog(@"%ld", arrDataSource.count);
        UIScrollView *scrollSmall = [[UIScrollView alloc] initWithFrame:CGRectMake(kWidth * i, 0, kWidth, kHeight)];
        scrollSmall.delegate = self;
        UIImageView *imagePhoto = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, scrollSmall.frame.size.width, scrollSmall.frame.size.height)];
        scrollSmall.minimumZoomScale = zoomMin;
        scrollSmall.maximumZoomScale = zoomMax;
        [scrollSmall setZoomScale:scrollSmall.minimumZoomScale animated:YES];
        imagePhoto.image = [UIImage imageNamed:arrDataSource[i]];
        [scrollSmall addSubview:imagePhoto];
        [_scrollView addSubview:scrollSmall];
    }
    _scrollView.contentSize = CGSizeMake(kWidth * 6, 0);
    [self addSubview:_scrollView];
    
     self.pageContrl = [[UIPageControl alloc] initWithFrame:pageFrame];
    _pageContrl.numberOfPages = arrDataSource.count;
    _pageContrl.pageIndicatorTintColor = pagetitinColor;
    _pageContrl.currentPageIndicatorTintColor = curPageColor;
    [_pageContrl addTarget:self action:@selector(addPageControlWithImage:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:_pageContrl];
    
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView    {
    self.pageContrl.currentPage = scrollView.contentOffset.x / self.frame.size.width;
    if (scrollView.contentOffset.x / self.frame.size.width == self.pageContrl.numberOfPages - 1) {
        scrollView.contentOffset = CGPointZero;
    }
    
}
- (void)addPageControlWithImage:(UIPageControl *)pageControl {
    self.scrollView.contentOffset = CGPointMake(pageControl.currentPage * self.frame.size.width, 0);
    if (pageControl.currentPage == 5) {
        self.pageContrl.currentPage = 0;
        self.scrollView.contentOffset = CGPointZero;
    }
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return [scrollView.subviews firstObject];
}
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale {
    scrollView.zoomScale = 1;
}



@end
